import {defineStore} from 'pinia'
import {useMenuNavApi} from '@/packages/admin/api/menu'
import {generateRoutes} from '@/router/routerMenu'
import type {RouteRecordRaw} from 'vue-router'

export const routerStore = defineStore('routerStore', {
    state: () => ({
        menuRoutes: [] as RouteRecordRaw[],
        searchMenu: [] as RouteRecordRaw[],
        routes: [] as RouteRecordRaw[],
        //第二级的菜单显示，用于顶部样式的显示
        subMenuRoutes: [] as RouteRecordRaw[],
    }),
    actions: {
        async getMenuRoutes() {
            const {data} = await useMenuNavApi()
            const routes = generateRoutes(data)
            this.menuRoutes.push(...routes)
            return this.menuRoutes
        },
        setSearchMenu(routers: RouteRecordRaw[]) {
            this.searchMenu = routers
        },
        setRoutes(routers: RouteRecordRaw[]) {
            this.routes = routers
        },
        setSubMenuRoutes(routers: RouteRecordRaw[]) {
            this.subMenuRoutes = routers
        }
    }
})
